package edu.gdin.ecommerce.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import edu.gdin.ecommerce.util.Pager;

@Repository("baseDAO")
public class BaseDAO {

	public HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	
	public void insert(Object obj) {
		this.hibernateTemplate.saveOrUpdate(obj);
	}
	
	public void delete(Object obj){
		this.hibernateTemplate.delete(obj);
	}
	
	public void update(Object obj){
		this.hibernateTemplate.saveOrUpdate(obj);
	}	
	
	public List<?> list(Class<?> clazz) {
		return this.hibernateTemplate.loadAll(clazz);
	}
	
	public List<?> query (String hql){
		return this.hibernateTemplate.find(hql);
	}
	
	
	public Object findByPk(Class<?> clazz ,Serializable pk){
		return this.hibernateTemplate.get(clazz, pk);
	}
	
	public Object findByPkForUpdate(Class<?> clazz ,Serializable pk){
		return this.hibernateTemplate.load(clazz, pk, LockMode.UPGRADE);
	}
	
	public Object loadByKey(Class<?> clazz, String keyName, Object keyValue) throws DataAccessException {
		List<?> result = this.hibernateTemplate.find(
				"from " + clazz.getName() + " where " + keyName + " = ?", keyValue);
		if (result != null && result.size() > 0) {
			return result.get(0);
		} else {
			return null;
		}
	}
	
	public List<?> getEntitysByKey(Class<?> clazz, String keyName, Object keyValue){
		List<?> result =  this.hibernateTemplate.find("from " + clazz.getSimpleName() +" n where n." + keyName + " ='" + keyValue + "'");
		if(result!=null && result.size()!=0)
			return result;
		else 
			return null;
	}
	
	
	public boolean isExist(Class<?> entity , String keyName ,Object keyValue){
		List<?> n = this.hibernateTemplate.find("from " + entity.getSimpleName() + " e where e." + keyName + " ='" + keyValue + "'");
		if(n.size()>=1)
			return true;
		else 
			return false;
	}
	
	/**
	 * Added By COF
	 * 2012-04-02
	 * @param entity
	 * @param keyName
	 * @param keyValue
	 * @return
	 */
	public boolean isExistByEntityName(String entity, String keyName ,Object keyValue){
		List<?> n = this.hibernateTemplate.find("from " + entity + " e where e." + keyName + " ='" + keyValue + "'");
		if(n.size()>=1)
			return true;
		else 
			return false;
	}
	
	
	public int getTotalSize(Class<?> clasz){
		return this.hibernateTemplate.loadAll(clasz).size();
	}
	
	// 待测试
	public int getTotalSize(String hql){
		List<?> list = this.hibernateTemplate.find("select count(*) " + hql.substring(hql.trim().indexOf("from")));
		if (list!=null && list.size()!=0) {
			Long total= (Long)list.get(0);
			return total.intValue();
		}
		return  0;
	}
	
	public int getTotalSizeBySql(String sql){
		Session session = this.hibernateTemplate.getSessionFactory().openSession();
		session.beginTransaction();
		int totalSize= session.createSQLQuery(sql).list().size();
		session.getTransaction().commit();
		session.close();
		return totalSize;
	}
	
	public Pager getPage(String hql ,int currentPage){
		int totalSize= this.getTotalSize(hql);
		Pager pager = new Pager(currentPage,totalSize);
		Session session = this.hibernateTemplate.getSessionFactory().openSession();
		session.beginTransaction();
		List<?> ls = (List<?>)session.createQuery(hql)		
		.setFirstResult((pager.getCurrentPage() - 1) * pager.getPageSize())
		.setMaxResults(pager.getPageSize())
		.list();
		session.getTransaction().commit();
		session.close();
		pager.setList(ls);
		return pager;
	}
	
	
	public Pager getPageBySql(String sql,int currentPage){
		int totalSize= this.getTotalSizeBySql(sql);
		Pager pager = new Pager(currentPage,totalSize);
		
		Session session = this.hibernateTemplate.getSessionFactory().openSession();
		session.beginTransaction();
		List<?> ls = (List<?>)session.createSQLQuery(sql)		
		.setFirstResult((pager.getCurrentPage() - 1) * pager.getPageSize())
		.setMaxResults(pager.getPageSize())
		.list();
		session.getTransaction().commit();
		session.close();
		pager.setList(ls);
		return pager;
	}
}
